/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic.service;

import dao.CalendarDAO;
import dao.ProjectDAO;
import entity.Calendar;
import entity.Project;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import vo.IValueObject;
import vo.ProjectVO;
import vo.SubjectVO;

/**
 *
 * @author zergio
 */
public class ProjectService implements IService {

    @Override
    public void persist(IValueObject vo, EntityManager em) {
        Project entity = new Project();
        entity.setId(((ProjectVO) vo).getId());
        entity.setName(((ProjectVO) vo).getName());
//        entity.setTasks(((ProjectVO) vo).getTasks());
        entity.setType(((ProjectVO) vo).getType());
        int calendarID = ((ProjectVO) vo).getCalendarId();
        Calendar cal = (Calendar) (new CalendarDAO().find(calendarID, em));
        entity.setCalendar(cal);
        cal.getProject().add(entity);

        new ProjectDAO().persist(entity, em);
    }

    @Override
    public IValueObject find(Object id, EntityManager em) {
        Project t = (Project) new ProjectDAO().find(id, em);
        if (t != null) {
            return t.toVO();
        } else {
            return null;
        }
    }

    @Override
    public void update(IValueObject vo, EntityManager em) {
        Project entity = new Project();
        entity.setId(((ProjectVO) vo).getId());
        entity.setName(((ProjectVO) vo).getName());
        entity.setType(((ProjectVO) vo).getType());

        new ProjectDAO().update(entity, em);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        new ProjectDAO().delete(id, em);
    }

    @Override
    public List<IValueObject> getList(EntityManager em) {
        List<IValueObject> projectVO = new ArrayList<>();
        for (Object project : new ProjectDAO().getList(em)) {
            projectVO.add(((Project) project).toVO());
        }
        return projectVO;
    }

    public List<ProjectVO> getProjectsByCalendar(Object id, EntityManager em) {
        List<Project> projects = new ArrayList<>();
        List<ProjectVO> projectVo = new ArrayList<>();
        try {
            projects = new ProjectDAO().getProjectsByCalendar(id, em);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Project p : projects) {
            projectVo.add(p.toVO());
        }
        return projectVo;
    }

    public void createProjectsFromSubjects(List<SubjectVO> subjects, EntityManager em) {
        for (SubjectVO subject : subjects) {
            if (new ProjectDAO().getProjectsByName(subject.getName(), em).isEmpty()) {
                Project entity = new Project();
                entity.setId(0);
                entity.setName(subject.getName());
                entity.setType(2);
                int calendarID = subject.getCalendarId();
                Calendar cal = (Calendar) (new CalendarDAO().find(calendarID, em));
                entity.setCalendar(cal);
                cal.getProject().add(entity);
                new ProjectDAO().persist(entity, em);
            }
        }
    }
}
